# -*- coding: utf-8 -*-
#
# Kate/Pâté plugins to work with code blocks
# Copyright 2010 by i.zaufi@gmail.com
#

import kate
import kate.gui

@kate.action('Insert char from line above', shortcut='Meta+E')
def insertCharFromLineAbove():
    document = kate.activeDocument()
    view = kate.activeView()
    currentPosition = view.cursorPosition()

    if currentPosition.line() == 0:
        return

    lineAbove = document.line(currentPosition.line() - 1)
    char = lineAbove[currentPosition.column():currentPosition.column() + 1]
    if not bool(char):
        return

    document.startEditing()
    document.insertText(currentPosition, char)
    document.endEditing()


@kate.action('Insert char from line below', shortcut='Meta+W')
def insertCharFromLineBelow():
    document = kate.activeDocument()
    view = kate.activeView()
    currentPosition = view.cursorPosition()

    if currentPosition.line() == 0:
        return

    lineBelow = document.line(currentPosition.line() + 1)
    char = lineBelow[currentPosition.column():currentPosition.column() + 1]
    if not bool(char):
        return

    document.startEditing()
    document.insertText(currentPosition, char)
    document.endEditing()


@kate.action('Kill text after cursor', shortcut='Meta+K')
def killRestOfLine():
    document = kate.activeDocument()
    view = kate.activeView()
    currentPosition = view.cursorPosition()

    line = document.line(currentPosition.line())[0:currentPosition.column()]

    document.startEditing()
    document.removeLine(currentPosition.line())
    document.insertLine(currentPosition.line(), line)
    view.setCursorPosition(currentPosition)
    document.endEditing()


@kate.action('Kill text before cursor', shortcut='Meta+U')
def killRestOfLine():
    document = kate.activeDocument()
    view = kate.activeView()
    currentPosition = view.cursorPosition()

    line = str(document.line(currentPosition.line()))
    spaceCount = len(line) - len(line.lstrip())
    line = ' ' * spaceCount + line[currentPosition.column():]

    document.startEditing()
    document.removeLine(currentPosition.line())
    document.insertLine(currentPosition.line(), line)
    currentPosition.setColumn(spaceCount)
    view.setCursorPosition(currentPosition)
    document.endEditing()
